home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / tcpsubr.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  7KB  |  337 lines

  1. /* Low level TCP routines:
  2.  *  control block management
  3.  *  sequence number logical operations
  4.  *  state transitions
  5.  *  RTT cacheing
  6.  *  garbage collection
  7.  *
  8.  * Copyright 1991 Phil Karn, KA9Q
  9.  */
  10. #include <stdio.h>
  11. #include "global.h"
  12. #include "timer.h"
  13. #include "mbuf.h"
  14. #include "netuser.h"
  15. #include "internet.h"
  16. #include "tcp.h"
  17. #include "ip.h"
  18.  
  19. /* TCP connection states */
  20. char *Tcpstates[] = {
  21.     "",
  22.     "Closed",
  23.     "Listen",
  24.     "SYN sent",
  25.     "SYN received",
  26.     "Established",
  27.     "FIN wait 1",
  28.     "FIN wait 2",
  29.     "Close wait",
  30.     "Last ACK",
  31.     "Closing",
  32.     "Time wait"
  33. };
  34.  
  35. /* TCP closing reasons */
  36. char *Tcpreasons[] = {
  37.     "Normal",
  38.     "Reset/Refused",
  39.     "Timeout",
  40.     "ICMP"
  41. };
  42. struct tcb *Tcbs;        /* Head of control block list */
  43. int16 Tcp_mss = DEF_MSS;    /* Maximum segment size to be sent with SYN */
  44. int32 Tcp_irtt = DEF_RTT;    /* Initial guess at round trip time */
  45. int Tcp_retries = DEF_RETRIES;    /* Max retries before resetting tcb */
  46. int Tcp_trace;          /* State change tracing flag */
  47. int Tcp_syndata;
  48. struct tcp_rtt Tcp_rtt[RTTCACHE];
  49. struct mib_entry Tcp_mib[] = {
  50.     NULLCHAR,        0,
  51.     "tcpRtoAlgorithm",    4,    /* Van Jacobsen's algorithm */
  52.     "tcpRtoMin",        0,    /* No lower bound */
  53.     "tcpRtoMax",        MAXINT32,    /* No upper bound */
  54.     "tcpMaxConn",        -1L,    /* No limit */
  55.     "tcpActiveOpens",    0,
  56.     "tcpPassiveOpens",    0,
  57.     "tcpAttemptFails",    0,
  58.     "tcpEstabResets",    0,
  59.     "tcpCurrEstab",        0,
  60.     "tcpInSegs",        0,
  61.     "tcpOutSegs",        0,
  62.     "tcpRetransSegs",    0,
  63.     NULLCHAR,        0,    /* Connection state goes here */
  64.     "tcpInErrs",        0,
  65.     "tcpOutRsts",        0,
  66. };
  67.  
  68.  
  69. /* Look up TCP connection
  70.  * Return TCB pointer or NULLTCB if nonexistant.
  71.  * Also move the entry to the top of the list to speed future searches.
  72.  */
  73. struct tcb *
  74. lookup_tcb(conn)
  75. register struct connection *conn;
  76. {
  77.     register struct tcb *tcb;
  78.     struct tcb *tcblast = NULLTCB;
  79.  
  80.     for(tcb=Tcbs;tcb != NULLTCB;tcblast = tcb,tcb = tcb->next){
  81.         /* Yet another structure compatibility hack */
  82.         if(conn->remote.port == tcb->conn.remote.port
  83.          && conn->local.port == tcb->conn.local.port
  84.          && conn->remote.address == tcb->conn.remote.address
  85.          && conn->local.address == tcb->conn.local.address){
  86.             if(tcblast != NULLTCB){
  87.                 /* Move to top of list */
  88.                 tcblast->next = tcb->next;
  89.                 tcb->next = Tcbs;
  90.                 Tcbs = tcb;
  91.             }
  92.             return tcb;
  93.         }
  94.  
  95.     }
  96.     return NULLTCB;
  97. }
  98.  
  99. /* Create a TCB, return pointer. Return pointer if TCB already exists. */
  100. struct tcb *
  101. create_tcb(conn)
  102. struct connection *conn;
  103. {
  104.     register struct tcb *tcb;
  105.     struct tcp_rtt *tp;
  106.  
  107.     if((tcb = lookup_tcb(conn)) != NULLTCB)
  108.         return tcb;
  109.     tcb = (struct tcb *)callocw(1,sizeof (struct tcb));
  110.     ASSIGN(tcb->conn,*conn);
  111.  
  112.     tcb->state = TCP_CLOSED;
  113.     tcb->cwind = tcb->mss = Tcp_mss;
  114.     tcb->ssthresh = 65535;
  115.     if((tp = rtt_get(tcb->conn.remote.address)) != NULLRTT){
  116.         tcb->srtt = tp->srtt;
  117.         tcb->mdev = tp->mdev;
  118.     } else {
  119.         tcb->srtt = Tcp_irtt;    /* mdev = 0 */
  120.     }
  121.     /* Initialize timer intervals */
  122.     set_timer(&tcb->timer,tcb->srtt);
  123.     tcb->timer.func = tcp_timeout;
  124.     tcb->timer.arg = tcb;
  125.  
  126.     tcb->next = Tcbs;
  127.     Tcbs = tcb;        
  128.     return tcb;
  129. }
  130.  
  131. /* Close our TCB */
  132. void
  133. close_self(tcb,reason)
  134. register struct tcb *tcb;
  135. int reason;
  136. {
  137.     struct reseq *rp1;
  138.     register struct reseq *rp;
  139.  
  140.     if(tcb == NULLTCB)
  141.         return;
  142.  
  143.     stop_timer(&tcb->timer);
  144.     tcb->reason = reason;
  145.  
  146.     /* Flush reassembly queue; nothing more can arrive */
  147.     for(rp = tcb->reseq;rp != NULLRESEQ;rp = rp1){
  148.         rp1 = rp->next;
  149.         free_p(rp->bp);
  150.         free((char *)rp);
  151.     }
  152.     tcb->reseq = NULLRESEQ;
  153.     setstate(tcb,TCP_CLOSED);
  154. }
  155.  
  156. /* Sequence number comparisons
  157.  * Return true if x is between low and high inclusive,
  158.  * false otherwise
  159.  */
  160. int
  161. seq_within(x,low,high)
  162. register int32 x,low,high;
  163. {
  164.     if(low <= high){
  165.         if(low <= x && x <= high)
  166.             return 1;
  167.     } else {
  168.         if(low >= x && x >= high)
  169.             return 1;
  170.     }
  171.     return 0;
  172. }
  173. int
  174. seq_lt(x,y)
  175. register int32 x,y;
  176. {
  177.     return (long)(x-y) < 0;
  178. }
  179. #ifdef    notdef
  180. int
  181. seq_le(x,y)
  182. register int32 x,y;
  183. {
  184.     return (long)(x-y) <= 0;
  185. }
  186. #endif    /* notdef */
  187. int
  188. seq_gt(x,y)
  189. register int32 x,y;
  190. {
  191.     return (long)(x-y) > 0;
  192. }
  193. int
  194. seq_ge(x,y)
  195. register int32 x,y;
  196. {
  197.     return (long)(x-y) >= 0;
  198. }
  199.  
  200. void
  201. setstate(tcb,newstate)
  202. register struct tcb *tcb;
  203. register int newstate;
  204. {
  205.     register char oldstate;
  206.  
  207.     oldstate = tcb->state;
  208.     tcb->state = newstate;
  209.     if(Tcp_trace)
  210.         printf("TCB %lx %s -> %s\n",ptol(tcb),
  211.          Tcpstates[oldstate],Tcpstates[newstate]);
  212.  
  213.     /* Update MIB variables */
  214.     switch(oldstate){
  215.     case TCP_CLOSED:
  216.         if(newstate == TCP_SYN_SENT)
  217.             tcpActiveOpens++;
  218.         break;
  219.     case TCP_LISTEN:
  220.         if(newstate == TCP_SYN_RECEIVED)
  221.             tcpPassiveOpens++;
  222.         break;
  223.     case TCP_SYN_SENT:
  224.         if(newstate == TCP_CLOSED)
  225.             tcpAttemptFails++; 
  226.         break;
  227.     case TCP_SYN_RECEIVED:
  228.         switch(newstate){
  229.         case TCP_CLOSED:
  230.         case TCP_LISTEN:
  231.             tcpAttemptFails++; 
  232.             break;
  233.         }
  234.         break;
  235.     case TCP_ESTABLISHED:
  236.     case TCP_CLOSE_WAIT:
  237.         switch(newstate){
  238.         case TCP_CLOSED:
  239.         case TCP_LISTEN:
  240.             tcpEstabResets++;
  241.             break;
  242.         }
  243.         tcpCurrEstab--;
  244.         break;
  245.     }
  246.     if(newstate == TCP_ESTABLISHED || newstate == TCP_CLOSE_WAIT)
  247.         tcpCurrEstab++;
  248.  
  249.     if(tcb->s_upcall)
  250.         (*tcb->s_upcall)(tcb,oldstate,newstate);
  251.  
  252.     switch(newstate){
  253.     case TCP_SYN_RECEIVED:    /***/
  254.     case TCP_ESTABLISHED:
  255.         /* Notify the user that he can begin sending data */
  256.         if(tcb->t_upcall)
  257.             (*tcb->t_upcall)(tcb,tcb->window - tcb->sndcnt);
  258.         break;
  259.     }
  260. }
  261. /* Round trip timing cache routines.
  262.  * These functions implement a very simple system for keeping track of
  263.  * network performance for future use in new connections.
  264.  * The emphasis here is on speed of update (rather than optimum cache hit
  265.  * ratio) since rtt_add is called every time a TCP connection updates
  266.  * its round trip estimate.
  267.  */
  268. void
  269. rtt_add(addr,rtt)
  270. int32 addr;        /* Destination IP address */
  271. int32 rtt;
  272. {
  273.     register struct tcp_rtt *tp;
  274.     int32 abserr;
  275.  
  276.     if(addr == 0)
  277.         return;
  278.     tp = &Tcp_rtt[(unsigned short)addr % RTTCACHE];
  279.     if(tp->addr != addr){
  280.         /* New entry */
  281.         tp->addr = addr;
  282.         tp->srtt = rtt;
  283.         tp->mdev = 0;
  284.     } else {
  285.         /* Run our own SRTT and MDEV integrators, with rounding */
  286.         abserr = (rtt > tp->srtt) ? rtt - tp->srtt : tp->srtt - rtt;
  287.         tp->srtt = ((AGAIN-1)*tp->srtt + rtt + (AGAIN/2)) >> LAGAIN;
  288.         tp->mdev = ((DGAIN-1)*tp->mdev + abserr + (DGAIN/2)) >> LDGAIN;
  289.     }
  290. }
  291. struct tcp_rtt *
  292. rtt_get(addr)
  293. int32 addr;
  294. {
  295.     register struct tcp_rtt *tp;
  296.  
  297.     if(addr == 0)
  298.         return NULLRTT;
  299.     tp = &Tcp_rtt[(unsigned short)addr % RTTCACHE];
  300.     if(tp->addr != addr)
  301.         return NULLRTT;
  302.     return tp;
  303. }
  304.  
  305. #ifndef UNIX
  306.  
  307. /* TCP garbage collection - called by storage allocator when free space
  308.  * runs low. The send and receive queues are crunched. If the situation
  309.  * is red, the resequencing queue is discarded; otherwise it is
  310.  * also crunched.
  311.  */
  312. void
  313. tcp_garbage(red)
  314. int red;
  315. {
  316.     register struct tcb *tcb;
  317.     struct reseq *rp,*rp1;
  318.  
  319.     for(tcb = Tcbs;tcb != NULLTCB;tcb = tcb->next){
  320.         mbuf_crunch(&tcb->rcvq);
  321.         mbuf_crunch(&tcb->sndq);
  322.         for(rp = tcb->reseq;rp != NULLRESEQ;rp = rp1){
  323.             rp1 = rp->next;
  324.             if(red){
  325.                 free_p(rp->bp);
  326.                 free((char *)rp);
  327.             } else {
  328.                 mbuf_crunch(&rp->bp);
  329.             }
  330.         }
  331.         if(red)
  332.             tcb->reseq = NULLRESEQ;
  333.     }
  334. }
  335.  
  336. #endif
  337.